/**
 * Render Bender - A Flash-based chained Pixel Bender kernel previewer
 * Copyright (C) 2009  David Lenaerts
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author information:
 * David Lenaerts
 * url: http://www.derschmale.com
 * mail: david.lenaerts@gmail.com
 * 
 */
 
package com.derschmale.renderBender.view
{
	import com.derschmale.renderBender.ApplicationFacade;
	import com.derschmale.renderBender.view.components.ControlPanel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ControlPanelMediator extends Mediator
	{
		public static const NAME : String = "ControlPanelMediator";
		
		public function ControlPanelMediator(panel : ControlPanel)
		{
			super(NAME, panel);
			panel.startButton.addEventListener(MouseEvent.CLICK, handleStartClick); 
			panel.stopButton.addEventListener(MouseEvent.CLICK, handleStopClick);
			panel.renderButton.addEventListener(MouseEvent.CLICK, handleRenderClick);
			panel.resetButton.addEventListener(MouseEvent.CLICK, handleResetClick);
			panel.asynchronousCheck.addEventListener(Event.CHANGE, handleAsynchronousCheck);
			panel.importButton.addEventListener(MouseEvent.CLICK, handleImportClick);
			//panel.maxFPS.addEventListener(Event.CHANGE, handleFPSChange);
		}
		
		public function get panel() : ControlPanel
		{
			return viewComponent as ControlPanel;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ ApplicationFacade.SEQUENCE_CREATED ];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName()) {
				case ApplicationFacade.SEQUENCE_CREATED:
					panel.startButton.enabled = true;
					panel.stopButton.enabled = false;
					break;
			}
		}
		
		private function handleStartClick(event : MouseEvent) : void
		{
			sendNotification(ApplicationFacade.START_SEQUENCE);
		}
		
		private function handleStopClick(event : MouseEvent) : void
		{
			sendNotification(ApplicationFacade.STOP_SEQUENCE);
		}
		
		/* private function handleFPSChange(event : Event) : void
		{
			sendNotification(ApplicationFacade.SET_FRAMERATE, panel.maxFPS.value);
		} */
		
		private function handleRenderClick(event : MouseEvent) : void
		{
			sendNotification(ApplicationFacade.EXECUTE_SEQUENCE);
		}
		
		private function handleResetClick(event : MouseEvent) : void
		{
			sendNotification(ApplicationFacade.RESET_ASSETS);
		}
		
		private function handleAsynchronousCheck(event : Event) : void
		{
			sendNotification(ApplicationFacade.SET_ASYNCHRONOUS, panel.asynchronousCheck.selected);
		}
		
		private function handleImportClick(event : Event) : void
		{
			sendNotification(ApplicationFacade.REQUEST_IMPORT_XML);
		}
	}
}